package com.yunhe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhe.common.QueryPageParam;
import com.yunhe.common.Result;
import com.yunhe.entity.Storage;
import com.yunhe.service.StorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
/**
 * 控制层：调用业务层
 */
@RestController
@RequestMapping("/storage")
@Api(value = "/storage",description = "仓库信息管理")
public class StorageController {
    @Autowired
    StorageService storageService;

    @PostMapping("/save")
    @ApiOperation("增加仓库信息")
    public Result save(@RequestBody Storage storage){
        return storageService.save(storage)?Result.success():Result.fail();
    }

    @PutMapping("/update")
    @ApiOperation("编辑仓库信息")
    public Result update(@RequestBody Storage storage){
        return storageService.updateById(storage)?Result.success():Result.fail();
    }

    @DeleteMapping("/del")
    @ApiOperation("根据仓库id删除仓库信息")
    public Result del(String id){
        return storageService.removeById(id)?Result.success():Result.fail();
    }

    /**
     * 常规刷新的查询；模糊查询；
     * 参数：参数类型 形参名
     * @return
     */
    @PostMapping("/listPage")
    @ApiOperation("分页查询和模糊查询 仓库信息")
    public Result listPage(@RequestBody QueryPageParam query){
        //从前端获取数据,param里面的name
        HashMap param = query.getParam();
        String name= (String) param.get("name");

        //创建分页对象，然后设置页码和页数；
        //获取页码和每页显示的条数,从请求参数query中获取
        Page<Storage> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        //创建查询包装器对象，基于lambda格式
        LambdaQueryWrapper<Storage>queryWrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name)&&!"null".equals(name))
            queryWrapper.like(Storage::getName, name);

        //查询，调用业务层对象查询；原生格式
        Page<Storage> result = storageService.page(page, queryWrapper);

        return Result.success(result.getRecords(),result.getTotal());

    }
    @GetMapping("/list")
    public Result list(){
        return Result.success(storageService.list());
    }
}